import {getEnergySource, WORKSTATE, workSwitch} from "./public/CreepPublicAction.js";

/**
 * 采集者工作逻辑
 * @param {Creep} creep
 * @param sourcesIndex
 */
export const harvesterRun = (creep, sourcesIndex) => {
    //工作逻辑转换

    workSwitch(creep, RESOURCE_ENERGY)
    if (creep.memory[WORKSTATE]) {
        workCore(creep, sourcesIndex)
    } else {
        getSources(creep, sourcesIndex)
    }
};

/**
 * 核心工作逻辑
 * @param {Creep} creep
 * @param sourcesIndex
 */
const workCore = (creep, sourcesIndex) => {
    getSources(creep, sourcesIndex)
    // const containers = getAppointFreeStructure(creep, Structure)
    // if (containers.length > 0) {
    //     getSources(creep)
    // } else {
    //     const targets = creep.room.find(FIND_STRUCTURES, {
    //         filter: (structure) => {
    //             return (structure.structureType === STRUCTURE_STORAGE) &&
    //                 structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
    //         }
    //     });
    //     const firstTarget = creep.room.find(FIND_STRUCTURES, {
    //         filter: (structure) => {
    //             return (structure.structureType === STRUCTURE_EXTENSION ||
    //                     structure.structureType === STRUCTURE_SPAWN ||
    //                     structure.structureType === STRUCTURE_TOWER) &&
    //                 structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
    //         }
    //     });
    //     //先填充extention和spawn，填充完成后再填充其它
    //     if (firstTarget.length > 0) {
    //         if (creep.transfer(firstTarget[0], RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
    //             creep.moveTo(firstTarget[0]);
    //         }
    //     } else {
    //         if (creep.transfer(targets[0], RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
    //             creep.moveTo(targets[0]);
    //         }
    //     }
    // }
}

/**
 * 资源获取逻辑
 * @param {Creep} creep
 * @param sourcesIndex
 */
const getSources = (creep, sourcesIndex) => {
    // //找到房间内的资源
    // const sources = creep.room.find(FIND_SOURCES);
    // // const dropSource = creep.room.find(FIND_DROPPED_RESOURCES);
    // if (sources.length > 0) {
    //
    //     if (creep.harvest(sources[0]) === ERR_NOT_IN_RANGE) {
    //         creep.moveTo(sources[0]);
    //     }
    // }
    // getContainerSources(creep,1000,1,'#f6007c')
    getEnergySource(creep, sourcesIndex, '#f6007c')
}